package com.chuanjiabao.wisdom.ui.activity.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.bean.ReasionInfoCallbackBean;
import com.chuanjiabao.wisdom.bean.WxBandBackBean;
import com.chuanjiabao.wisdom.bean.WxLoginCallBackBean;
import com.chuanjiabao.wisdom.databinding.ActivityInputLoginPwdBinding;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.GsonUtil;
import com.chuanjiabao.wisdom.util.HTTPAPI;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.StringUtil;
import com.chuanjiabao.wisdom.util.ToastUtil;
import com.chuanjiabao.wisdom.util.timeutil.GetCodeTimeUtil;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;

public class InputLoginPwdActivity extends BaseActivity {

    private ActivityInputLoginPwdBinding binding;
    private GetCodeTimeUtil timeUtil;
    private String phone, code, unionid, loginType;
    private WxLoginCallBackBean backBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_input_login_pwd);
        binding.setActivity(this);
        initView();
        getCode();
    }

    private void initView() {
        phone = getIntent().getStringExtra("module_phone");
        unionid = getIntent().getStringExtra("module_unionid");
        loginType = getIntent().getStringExtra("module_login_type");

        if ("bandPhone".equals(loginType)) {
            backBean = (WxLoginCallBackBean) SpUtils.getBean("module_wx_bean");
        }

        binding.ivModuleClose.setOnClickListener(this);
        binding.tvModuleOk.setOnClickListener(this);
        binding.tvModuleSendCode.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleClose:
                onBackPressed();
                break;
            case R.id.tvModuleSendCode:
                getCode();
                break;
            case R.id.tvModuleOk:
                code = binding.vcvModule.getEditContent();
                if (TextUtils.isEmpty(code)) {
                    ToastUtil.showToast("请输入验证码");
                    return;
                }

                if ("phoneLogin".equals(loginType)) {
                    login(code);
                } else if ("bandPhone".equals(loginType)) {
                    wxBand(code);
                }
                break;
        }
    }

    private void getCode() {
        HTTPAPI.getInstance().getCode(phone, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputLoginPwdActivity.this, s);
                } else {
                    timeUtil = new GetCodeTimeUtil(60000, 1000, binding.tvModuleSendCode);
                    timeUtil.start();
                }
            }
        });
    }

    private void login(String code) {
        HTTPAPI.getInstance().login(phone, code, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputLoginPwdActivity.this, s);
                } else {
                    WxBandBackBean bean = GsonUtil.GsonToBean(s, WxBandBackBean.class);
                    SpUtils.put("module_token", bean.getData().getToken());
                    getUserInfo(bean.getData());
                }
            }
        });
    }

    private void wxBand(String code) {
        HTTPAPI.getInstance().wxBand(backBean.getUnionid(), backBean.getOpenid(),
                phone, code, new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (!StringUtil.isFail(s)) {
                            StringUtil.showFail(InputLoginPwdActivity.this, s);
                        } else {
                            WxBandBackBean backBean = GsonUtil.GsonToBean(s, WxBandBackBean.class);
                            SpUtils.put("module_token", backBean.getData().getToken());
                            getUserInfo(backBean.getData());
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timeUtil != null) {
            timeUtil.onCancel();
        }
    }

    private void getUserInfo(final WxBandBackBean.DataBean dataBean) {
        HTTPAPI.getInstance().getUserInfo(dataBean.getToken(), new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (!StringUtil.isFail(s)) {
                    StringUtil.showFail(InputLoginPwdActivity.this, s);
                } else {
                    ReasionInfoCallbackBean bean = GsonUtil.GsonToBean(s, ReasionInfoCallbackBean.class);
                    SpUtils.putBean("module_reasion", bean.getData());
                    SpUtils.put("module_userId", bean.getData().getId() + "");
                    SpUtils.put("module_truename", bean.getData().getTruename());
                    SpUtils.put("module_userinfo_head", bean.getData().getAvatar());

                    int infoFinish = bean.getData().getInfo_finish();
                    startActivity(
                            new Intent(
                                    InputLoginPwdActivity.this,
                                    infoFinish == 1 ? MainActivity.class : InputPersionInfoActivity.class)
                    );

                    ActivityStack.getScreenManager()
                            .popActivities(
                                    UserNumLoginActivity.class,
                                    InputLoginPwdActivity.class
                            );
                }
            }
        });
    }

}
